package com.miage.m1.xml;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

public class Person {

	private final int id;
	private final String firstName;
	private final String lastName;
	private final String nationality;
	private final Date birthday;
	private String phone;
	private double pay;

	protected AttributesImpl atts = new AttributesImpl();

	/* Constructors */

	/**
	 * Create a Person
	 * @param lastName
	 * @param firstName
	 * @param nationality
	 * @param birthday
	 * @param phone : phone number exemple <code>"+00 0 00 00 00 00"</code>
	 * @param pay
	 */
	public Person(int id, String lastName, String firstName, String nationality, Date birthday, String phone, double pay){
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.nationality = nationality;
		this.birthday = birthday;
		this.phone = phone;
		this.pay = pay;
		
		System.out.println(birthday);

	}


	/**
	 *Create a <code>Person</code> without a phone number and a pay
	 * @param id : identificator of a Person
	 * @param firstName
	 * @param lastName
	 * @param nationality
	 * @param birthday
	 */
	public Person(int id, String firstName, String lastName, Date birthday){
		this(id, firstName, lastName, "", birthday, "", 0);
	}


	/* Getters */

	public int getId(){ return this.id; }
	
	public String getLastName(){ return this.lastName; }

	public String getFirstName(){ return this.firstName; }

	public String getNationality(){ return this.nationality; }

	public Date getBirthday() { return birthday; }

	public double getPay(){ return this.pay; }

	public String getPhone() { return phone; }

	public void setPhone(String phone) { this.phone = phone; }
	
	public void setPay(double pay){ this.pay = pay; }



	/* Methods and Properties*/

	/**
	 * Serialize a Person
	 * @param handler
	 * @return
	 * @throws SAXException
	 */
	public ContentHandler Serialize(ContentHandler handler) throws SAXException{
		//TODO Create the serialization of a Player.
		if(handler == null){
			throw new SAXException("Pas de ContentHandler");
		}
		atts.clear();

		handler.startElement("", "firstName", "firstName", atts);
		char[] firstName = this.getFirstName().toCharArray();
		handler.characters(firstName,0,firstName.length);
		handler.endElement("", "firstName", "firstName");

		handler.startElement("", "lastName", "lastName", atts);
		char[] lastName = this.getLastName().toCharArray();
		handler.characters(lastName,0,lastName.length);
		handler.endElement("", "lastName", "lastName");

		handler.startElement("", "nationality", "nationality", atts);
		char[] nationality = this.getNationality().toCharArray();
		handler.characters(nationality,0,nationality.length);
		handler.endElement("", "nationality", "nationality");

		handler.startElement("", "age", "age", atts);
		char[] age = String.valueOf(this.getAge()).toCharArray();
		handler.characters(age, 0, age.length);
		handler.endElement("", "age", "age");

		handler.startElement("", "phone", "phone", atts);
		char[] phone = this.getPhone().toCharArray();
		handler.characters(phone, 0, phone.length);
		handler.endElement("", "phone", "phone");

		handler.startElement("", "pay", "pay", atts);
		char[] pay = String.valueOf(this.getPay()).toCharArray();
		handler.characters(pay, 0, pay.length);
		handler.endElement("", "pay", "pay");


		return handler;
	}

	public int getAge(){
		Calendar curr = Calendar.getInstance();
		Calendar birth = Calendar.getInstance();
		birth.setTime(this.birthday);
		int yeardiff = curr.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
		curr.add(Calendar.YEAR,-yeardiff);
		if(birth.after(curr))
		{
		    yeardiff = yeardiff - 1;
		 }
		  return yeardiff;
	}
}